﻿#pragma once
#include "OpenGL项目主标头.h"
#include "OpenGL测试类标头.h"
#include "OpenGL渲染器类标头.h"
#include "OpenGL纹理类标头.h"
#include "OpenGL纹理测试类标头.h"
// C/C++ 标准库学习网站：https://legacy.cplusplus.com
// C/C++ 参考学习网站：https://zh.cppreference.com/w/cpp
// C/C++ 微软(Visual Studio)中文学习网站：https://learn.microsoft.com/zh-cn/cpp/cpp/?view=msvc-170
// OpenGl 学习网站：https://www.khronos.org/opengl/wiki
// OpenGl 的 GLFW 学习网站：https://www.glfw.org/docs/latest/glfw3_8h.html#ac06b663d79c8fcf04669cc8fcc0b7670
// OpenGl 的 GLEW 学习网站：https://docs.gl/

namespace my_test {
	TestTexture2D::TestTexture2D(uint32_t id)
		: position2D{
			//-0.5f, -0.5f, 0.0f, 0.0f,
			// 0.5f, -0.5f, 1.0f, 0.0f,
			// 0.5f,  0.5f, 1.0f, 1.0f,
			//-0.5f,  0.5f, 0.0f, 1.0f
			440.0f, 280.0f, 0.0f, 0.0f,
			840.0f, 280.0f, 1.0f, 0.0f,
			840.0f, 680.0f, 1.0f, 1.0f,
			440.0f, 680.0f, 0.0f, 1.0f
		},
		indices2D{
			0, 1, 2,
			2, 3, 0
		},
		m_id(id)
	{
		pvm.projection = glm::ortho(0.0f, 1280.0f, 0.0f, 960.0f, -440.0f, 0.0f);
		pvm.model = pvm.view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));

		/*gl_call(glGenVertexArrays(id, &vertex_array_id));
		gl_call(glBindVertexArray(vertex_array_id));
		cout << "TestTexture2D VAO ID: " << vertex_array_id << endl;*/

		/*gl_call(glGenBuffers(id, &vertex_buffer));
		gl_call(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)); // 绑定 VBO 缓冲
		gl_call(glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float), position2D, GL_STATIC_DRAW));
		cout << "TestTexture2D VBO ID: " << vertex_buffer << endl;*/

		layout.push<float>(2); // 位置
		layout.push<float>(2); // 纹理坐标
		vertex_array.add_buffer(vertex_buffer, layout);
		/*gl_call(glEnableVertexAttribArray(0)); // 启用顶点属性，0 是索引，对应 glVertexAttribPointer 的第一个参数
		gl_call(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (const void*)0)); */

		/*gl_call(glGenBuffers(id, &index_buffer));
		gl_call(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer)); // 绑定 EBO 缓冲
		gl_call(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(position2D), indices2D, GL_STATIC_DRAW));
		cout << "TestTexture2D EBO ID: " << index_buffer << endl; */

		shader = std::make_unique<Shader>("Resource/shaders/MyTestShaders.shader");
		shader->get_source_code().print();
		shader->use_program();
		int i = shader->get_uniform_location("our_color");
		//printf("i = %d\n", i);
		shader->set_uniform4f("our_color", 0.8f, 0.4f, 0.0f, 1.0f);

		texture = std::make_unique<Texture>("Resource/textures/C.png", 1);
		texture->bind();
		shader->set_uniform1i("u_texture", 1);
	}

	void TestTexture2D::bind() {
		vertex_array.bind();
		vertex_buffer.bind();
		index_buffer.bind();
		/*glBindVertexArray(vertex_array_id);
		glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); // 绑定 VBO 缓冲
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); // 绑定 EBO 缓冲 */
		shader->use_program();
		texture->bind();
	}

	void TestTexture2D::unbind() {
		gl_call(glBindVertexArray(0));
		gl_call(glBindBuffer(GL_ARRAY_BUFFER, 0));
		gl_call(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
		shader->not_use_program();
		gl_call(glBindTexture(GL_TEXTURE_2D, 0));
	}

	void TestTexture2D::up_date(float delta_time) {}

	void TestTexture2D::render() {
		ImGui::SliderFloat3("translation_test", /*(float*)*/&translation[0], -440.0f, 440.0f);
		ImGui::SliderFloat("test alpha", /*(float*)*/&m_alpha, 0.0f, 1.0f);
		set_model_translation(pvm.view, translation);
		bind();
		shader->use_program();
		shader->set_uniform_matrix4fv("mvp", pvm[0] * pvm[1] * pvm[2]);
		shader->set_uniform1i("u_texture", 0);
		draw();
	}

	void TestTexture2D::imgui_render() {

	}

	void TestTexture2D::set_model_translation(glm::mat4 mat4, glm::vec3 vec3) {
		pvm.model = glm::translate(mat4, vec3);
	}

	void TestTexture2D::set_view_translation(glm::mat4 mat4, glm::vec3 vec3) {
		pvm.view = glm::translate(mat4, vec3);
	}

	void TestTexture2D::draw() {
		gl_call(glDrawElements(GL_TRIANGLES, sizeof(indices2D) / sizeof(float), GL_UNSIGNED_INT, nullptr));
	}
}
